package com.songcha.sakura.ui.view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.songcha.library_common.util.dp2px
import com.songcha.library_common.util.sp2px
import com.songcha.sakura.R
import com.songcha.sakura.util.*

fun interface IOnSeekChangeListener{
    fun onSeek(percent:Int)
}


class VideoSeekBar: View {
    private val PB_DEFAULT_WIDTH=400f
    private val PB_DEFAULT_HEIGHT=40f
    private var mBackGroundColor=Color.TRANSPARENT
    private var mProgressBackGroundColor=Color.GREEN
    private var mProgress=0
    private var mBgPaint=Paint()
    private var mProgressBgPaint=Paint()
    private var mProgressTextPaint=Paint()
    private var mOnSeekListener:IOnSeekChangeListener?=null
    private var mBgRect=RectF()
    private var mProgressBgRect=RectF()

    constructor(context: Context) : super(context) {
        initView(context,null)
    }

    constructor(context: Context, attrs: AttributeSet):this(context,attrs,0){

    }

    constructor(context: Context, attrs: AttributeSet, defStyle:Int):this(context,attrs,defStyle,0){

    }

    constructor(context: Context, attrs: AttributeSet, defStyle:Int, defRes:Int):super(context,attrs,defStyle,defRes){
        initView(context,attrs)
    }

    private fun initView(context: Context, attrs: AttributeSet?){
        isClickable=true

        if(attrs!=null){
            val ta=context.obtainStyledAttributes(attrs, R.styleable.VideoSeekBar)
            mBackGroundColor=ta.getColor(R.styleable.VideoSeekBar_pb_background,Color.parseColor("#7ff2f2f2"))
            mProgressBackGroundColor=ta.getColor(R.styleable.VideoSeekBar_pb_progress_background,Color.GREEN)
            ta.recycle()
        }

        mBgPaint.style=Paint.Style.FILL
        mBgPaint.color=mBackGroundColor

        mProgressBgPaint.style=Paint.Style.FILL
        mProgressBgPaint.color=mProgressBackGroundColor


        mProgressTextPaint.color=Color.WHITE
        mProgressTextPaint.textSize=16f.sp2px()


    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        val widthMode=MeasureSpec.getMode(widthMeasureSpec)
        var widthSize=MeasureSpec.getSize(widthMeasureSpec)
        val heightMode=MeasureSpec.getMode(heightMeasureSpec)
        var heightSize=MeasureSpec.getSize(heightMeasureSpec)

        if(widthMode!=MeasureSpec.EXACTLY){
            widthSize=PB_DEFAULT_WIDTH.toInt()
        }
        if(heightMode!=MeasureSpec.EXACTLY){
            heightSize=PB_DEFAULT_HEIGHT.toInt()
        }
        setMeasuredDimension(widthSize,heightSize)
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
         mBgRect=RectF(0f,10f.dp2px(),measuredWidth.toFloat(),measuredHeight.toFloat())
         mProgressBgRect=RectF(0f,10f.dp2px(),0f,measuredHeight.toFloat())
    }

    fun setProgress(progress:Int){
        var p=progress
        if(p<0) p=0
        else if(p>100) p=100

        if(mProgress!=p){
            mProgress=progress
            invalidate()
        }
    }

    fun setOnSeekListener(listener:IOnSeekChangeListener){
        mOnSeekListener=listener
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        if(event!=null){
            if(event.action==MotionEvent.ACTION_UP && mOnSeekListener!=null){
                val p=(event.x*1.0f/width*100).toInt()

                if(mProgress!=p){
                    mProgress=p
                    invalidate()
                }

                mOnSeekListener!!.onSeek(p)
                return true
            }
        }

        return super.onTouchEvent(event)
    }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        if(canvas==null)  return
        //val bgRect=RectF(0f,10f.dp2px(),width.toFloat(),height.toFloat())
        //canvas.drawRoundRect(bgRect,(height/2).toFloat(),(height/2).toFloat(),mBgPaint)
        canvas.drawRect(mBgRect,mBgPaint)
        //val progressBgRect=RectF(0f,10f.dp2px(),(width*mProgress/100).toFloat(),height.toFloat())
        mProgressBgRect.right=(width*mProgress/100).toFloat()
        canvas.drawRect(mProgressBgRect,mProgressBgPaint)

    }


}